/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * TrainingBook
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * Projects is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Projects is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <activity-notebook.hpp>
#include <filesystem-utils.hpp>
 
 
///@brief Default constructor
ActivityNotebook::ActivityNotebook() : Gtk::Notebook()
{
    append_page(summaryVBox, "Résumé");
    append_page(mapVBox, "Carte");
    append_page(editorVBox, "Editeur");
    
    sum3ColumnsHBox.pack_start(sumTotalsVBox, Gtk::PACK_EXPAND_WIDGET);
    sum3ColumnsHBox.pack_start(sumAveragesVBox, Gtk::PACK_EXPAND_WIDGET);
    sum3ColumnsHBox.pack_start(sumMetricsVBox, Gtk::PACK_EXPAND_WIDGET);
    
    
    summaryVBox.pack_start(sumDateLabel, Gtk::PACK_EXPAND_WIDGET);
    summaryVBox.pack_start(sumLocationLabel, Gtk::PACK_EXPAND_WIDGET);
    summaryVBox.pack_start(sum3ColumnsHBox, Gtk::PACK_EXPAND_WIDGET);
    summaryVBox.pack_start(sumIntervalesVBox, Gtk::PACK_EXPAND_WIDGET);
    summaryVBox.pack_start(recomputeAvPowerButton, Gtk::PACK_EXPAND_WIDGET);
    
    
    sumTotalsVBox.pack_start(sumTotalsColumnLabel, Gtk::PACK_EXPAND_WIDGET);
    sumTotalsVBox.pack_start(sumDistanceLabel, Gtk::PACK_EXPAND_WIDGET);
    sumTotalsVBox.pack_start(sumDurationLabel, Gtk::PACK_EXPAND_WIDGET);
    
    sumAveragesVBox.pack_start(sumAveragesColumnLabel, Gtk::PACK_EXPAND_WIDGET);
    sumAveragesVBox.pack_start(sumAvgCadenceLabel, Gtk::PACK_EXPAND_WIDGET);
    sumAveragesVBox.pack_start(sumAvgPowerLabel, Gtk::PACK_EXPAND_WIDGET);
    sumAveragesVBox.pack_start(sumAvgHRLabel, Gtk::PACK_EXPAND_WIDGET);
    
    sumMetricsVBox.pack_start(sumMetricsColumnLabel, Gtk::PACK_EXPAND_WIDGET);
    
    // Set labels
    sumTotalsColumnLabel.set_markup("<big><b>Totaux</b></big>");
    sumAveragesColumnLabel.set_markup("<big><b>Moyennes</b></big>");
    sumMetricsColumnLabel.set_markup("<big><b>mesures</b></big>");
    
    // Set buttons
    recomputeAvPowerButton.set_label("Recalculer puissance moyenne");
    recomputeAvPowerButton.signal_clicked().connect(sigc::mem_fun(*this,
                                                                  &ActivityNotebook::on_button_recompute_av_power_clicked));
    
    
    haveModifs = false;
    show_all();
    
    //Refresh();
}

///@brief Destructor
ActivityNotebook::~ActivityNotebook()
{

}

///@brief Set the pointer to activity in edition
///@param a Pointer to an activity
void ActivityNotebook::SetActivity(Activity* a)
{
    activity = a;
}

///@brief Return true if the activity has been modified
bool ActivityNotebook::HaveModification()
{
    return haveModifs;
}

///@brief Refresh the content of the notebook with the current activity
void ActivityNotebook::Refresh()
{
    const std::string FRENCH_DAY[7] = {"Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"};
    
    std::cout << "Un rafraichissement ?\n";
    // Refresh the summary page
    if(activity)
    {
        sumDateLabel.set_markup("<big><b>" + FRENCH_DAY[activity->GetDateTime().GetDayOfWeek()-1] + " " + activity->GetDateTime().AsString("dd/mm/yyyy à hh:nn") + "</b></big>");
        sumLocationLabel.set_markup("<b>Lieu : " + activity->GetLocation() + "</b>");
        sumDistanceLabel.set_text("Distance (km) : " + DoubleToString(activity->GetDistance(),2));
        sumDurationLabel.set_text("Durée : " + activity->GetDuration().AsText());
        sumAvgCadenceLabel.set_text("Cadence moyenne (rpm) : " + UIntToString(activity->GetAverageCadence()));
        sumAvgPowerLabel.set_text("Puissance moyenne (W) : " + DoubleToString(activity->GetAveragePower(), 0));
        sumAvgHRLabel.set_text("FC moyenne (bpm) : " + UIntToString(activity->GetFCMoy()));
        
    }
    
}

///@brief Action when clicking on the button to recompute the average power of selected activity
void ActivityNotebook::on_button_recompute_av_power_clicked()
{
    std::cout << "avant : "<< haveModifs << "\n";
    if(activity)
    {
        if(activity->RecomputeAveragePower())
            haveModifs = true;
        Refresh();
    }
    std::cout << "apres : " << haveModifs << "\n";
}

///@brief Say to the notebook that the activity have no pending modifications
void ActivityNotebook::SetNoModif()
{
    haveModifs = false;
}